﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Win32;
using MAntLibrary;
using System.IO;
using System.Xml;
using System.Data.SqlClient;
using System.Net;

namespace MAntWeb
{
    public partial class index : System.Web.UI.Page
    {
        private IDodavatel IDodavatel;
        private Dodavatel Supplier;
        private List<ZboziNetDirect> seznam;

        XmlDocument doc = new XmlDocument();
        XmlNode root = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            IDodavatel = null;
            cmbDodavatel.Items.Add("Hitec");
        }

        protected void btnStart_Click(object sender, EventArgs e)
        {
            /*
            List<ZboziNetDirect> result = MAntLibrary.suppliers.Hitec.Test();

            foreach (ZboziNetDirect zbozi in result)
            {
                lblResult.Text += zbozi.IdZbozi;
            }

            string connectionString = "server=192.168.1.5;uid=db4009;pwd=!marwinfreeman1!;database=db4009";
            using (SqlConnection connection = new SqlConnection(connectionString)) 
            {    
              //int employeeID = findEmployeeID();    
              try    
              {

                        connection.Open();
                        using (SqlCommand command = new SqlCommand("SELECT TOP 2 * FROM Dogs1", connection))
	                    using (SqlDataReader reader = command.ExecuteReader())
	                    {
		                    while (reader.Read())
		                    {
		                        Console.WriteLine("{0} {1} {2}",
			                    reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
		                    }
	                    }
               } 
               catch (Exception) 
               { 
               }
            }
            */
    
            Supplier = new Dodavatel(cmbDodavatel.SelectedValue, 1);
            //IDodavatel = (IDodavatel)System.Activator.CreateInstance(Type.GetType("MAntLibrary.suppliers." + Supplier.JmenoDodavatele));
            IDodavatel = (IDodavatel)System.Activator.CreateInstance(typeof(MAntLibrary.suppliers.Hitec));

            // Zjistim cestu k souboru
            string paaaath = Server.MapPath(FileUpload1.FileName);
            FileInfo file = new FileInfo(paaaath);

            // Nahraju na FTP
            Upload2FTP(file, paaaath);

            try
            {
                MAntLibrary.suppliers.Hitec H = new MAntLibrary.suppliers.Hitec();
                H.CtiXml(file, Supplier);
            }
            catch (Exception ex)
            {
                //IOproudy.PridejChybu(MethodInfo.GetCurrentMethod().Name, e.ToString(), DateTime.Now);
                return;
            }
        }

        /// <summary>
        /// nahraje XML soubory na FTP
        /// </summary>
        /// <param name="directoryPATH"></param>
        public void Upload2FTP(FileInfo file, string path)
        {
            string CompleteDPath = "ftp://www.hromek.aspone.cz/users/vala/uploaded_files/";
            WebRequest reqObj = WebRequest.Create(CompleteDPath + file.Name);

            try
            {
                FtpWebRequest ftpClient = (FtpWebRequest)FtpWebRequest.Create(reqObj.RequestUri);
                ftpClient.Credentials = new System.Net.NetworkCredential("hromek.aspone.cz", "marwinfreeman1");
                ftpClient.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
                ftpClient.UseBinary = true;
                ftpClient.KeepAlive = true;
                ftpClient.ContentLength = file.FullName.Length;
                byte[] buffer = new byte[4097];
                int bytes = 0;
                int total_bytes = (int)file.FullName.Length;
                using (FileStream fs = File.OpenRead(path))
                {
                    using (Stream rs = ftpClient.GetRequestStream())
                    {
                        while (total_bytes > 0)
                        {
                            bytes = fs.Read(buffer, 0, buffer.Length);
                            rs.Write(buffer, 0, bytes);
                            total_bytes = total_bytes - bytes;
                        }
                    }
                }
                //fs.Flush();
                
                FtpWebResponse uploadResponse = (FtpWebResponse)ftpClient.GetResponse();
                string value = uploadResponse.StatusDescription;
                uploadResponse.Close();
            }
            catch (WebException e)
            {
                //IOproudy.PridejChybu(MethodInfo.GetCurrentMethod().Name, e.ToString(), DateTime.Now);
                return;
            }
            catch (UnauthorizedAccessException uae)
            {
                //IOproudy.PridejChybu(MethodInfo.GetCurrentMethod().Name, uae.ToString(), DateTime.Now);
                return;
            }
        }

    }
}